其他
还在手动部署 Spring Boot 项目?使用 Docker 真香!
docker介绍
安装docker
Ubuntu安装docker
CentOS安装docker
yum update
yum install -y yum-utils device-mapper-persistent-data lvm2
yum-config-manager --add-repo
https://download.docker.com/linux/centos/docker-ce.repo
yum update
yum install -y docker-ce
systemctl start docker
systemctl restart docker
systemctl stop docker
systemctl enable docker
systemctl status docker
通过脚本安装
curl -fsSL https://get.docker.com/ | sh
wget -qO- https://get.docker.com/ | sh
拉取java环境
docker pull java:8
docker images
创建springboot项目
<properties>
<docker.image.prefix>registry.aliyuncs.com/linhuatest</docker.image.prefix>
</properties>
<!-- docker插件 -->
<plugin>
<groupId>com.spotify</groupId>
<artifactId>docker-maven-plugin</artifactId>
<version>1.0.0</version>
<configuration>
<imageName>${docker.image.prefix}/${project.artifactId}</imageName>
<!--docker文件所在的目录-->
<dockerDirectory>src/main/docker</dockerDirectory>
<resources>
<resource>
<targetPath>/</targetPath>
<directory>${project.build.directory}</directory>
<include>${project.build.finalName}.jar</include>
</resource>
</resources>
</configuration>
</plugin>
FROM java:8
VOLUME /tmp/tomcat
ADD spring-boot-docker-0.0.1-SNAPSHOT.jar springboot-docker.jar
ENTRYPOINT ["java","-Djava.security.egd=file:/dev/./urandom","-jar","/springboot-docker.jar"]
FROM:指定存在的镜像,java:8是我刚刚拉取的镜像,运行的基础。VOLUME:指向的一个临时文件,用于存储tomcat工作。ADD:复制文件并且重命名文件。ENTRYPOINT:初始化配置或者自定义配置。
package com.ymy.controller;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;
@RestController
@Slf4j
public class TestController {
@RequestMapping(value = "/test",method = RequestMethod.GET)
public String test(){
System.out.println("这是控制台日志!");
log.info("这是输出到文件的日志");
return "HELLO-BUG!!!!!!!!!!";
}
}
server:
port: 9999
. ____ _ __ _ _
/\\ / ___'_ __ _ _(_)_ __ __ _ \ \ \ \ ( ( )\___ | '_ | '_| | '_ \/ _` | \ \ \ \
\\/ ___)| |_)| | | | | || (_| | ) ) ) )
' |____| .__|_| |_|_| |_\__, | / / / /
=========|_|==============|___/=/_/_/_/
:: Spring Boot :: (v2.2.5.RELEASE)
15:29:19.386 [main] INFO com.ymy.SpringBootDockerApplication - Starting SpringBootDockerApplication on LAPTOP-3GLHJRE9 with PID 20652 (D:\springboot\spring-boot-docker\target\classes started by admin in D:\springboot)
15:29:19.395 [main] INFO com.ymy.SpringBootDockerApplication - No active profile set, falling back to default profiles: default
15:29:20.183 [main] INFO org.springframework.boot.web.embedded.tomcat.TomcatWebServer - Tomcat initialized with port(s): 9999 (http)
15:29:20.200 [main] INFO org.apache.coyote.http11.Http11NioProtocol - Initializing ProtocolHandler ["http-nio-9999"]
15:29:20.201 [main] INFO org.apache.catalina.core.StandardService - Starting service [Tomcat]
15:29:20.201 [main] INFO org.apache.catalina.core.StandardEngine - Starting Servlet engine: [Apache Tomcat/9.0.31]
15:29:20.309 [main] INFO org.apache.catalina.core.ContainerBase.[Tomcat].[localhost].[/] - Initializing Spring embedded WebApplicationContext
15:29:20.309 [main] INFO org.springframework.web.context.ContextLoader - Root WebApplicationContext: initialization completed in 881 ms
15:29:20.452 [main] INFO org.springframework.scheduling.concurrent.ThreadPoolTaskExecutor - Initializing ExecutorService 'applicationTaskExecutor'
15:29:20.568 [main] INFO org.apache.coyote.http11.Http11NioProtocol - Starting ProtocolHandler ["http-nio-9999"]
15:29:20.596 [main] INFO org.springframework.boot.web.embedded.tomcat.TomcatWebServer - Tomcat started on port(s): 9999 (http) with context path ''
15:29:20.599 [main] INFO com.ymy.SpringBootDockerApplication - Started SpringBootDockerApplication in 1.664 seconds (JVM running for 4.04)
打包springboot到docker
mvn clean package docker:build
docker images
docker run --name springbooot-docker -p 9999:9999 -d 4a2
run:运行的意思–name:指定镜像启动的之后的名称-p:容器和外部的端口映射 第一个端口:外部 第二个端口:内部-d:后台运行 -t:实时运行,窗口关闭,程序结束。4a2:表示镜像的id(IMAGE ID)前3位,这里的id并不需要输入全称,只需要输入前几个就行,有一个前提:当有很多镜像的时候,前面几个字符就有可能会相同,这个时候就需要多输入几位,直到不相同位置。
docker ps
docker查看容器的日志
docker ps
docker logs -f --tail=100 ca
查看log4j2输出问文件日志
tail -100f info.log
docker exec -it ca2cd59fff9b /bin/bash
ca2cd59fff9b:容器id
cd work/spring-boot-docker
tail -100f info.log
exit
作者:流星007
toutiao.com/i6843391272229536267
【Stackoverflow 问题】为什么资深的程序员都不用 “ ! = null " 做判空?
你还在使用 BeanUtils 来做对象转换吗?快试试 MapStruct 吧!
Spring boot 集成阿里开源 Sentinel 限流神器,轻松搞定接口限流!
关于分布式锁 Redis 与 Zookeeper 的原理,它们如何实现分布式锁?
IntelliJ IDEA 2020.2.3 版本永久破解激活详细教程,亲测有效!
有了 HTTP 协议,为什么还要 RPC 协议,两者有什么区别?